package com.adfframeworks.labs.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.adfframeworks.labs.dao.RequestDAO;
import com.adfframeworks.labs.domain.Request;
import com.adfframeworks.labs.service.RequestService;

@Service("RequestService")
public class RequestServiceImpl implements RequestService {

	private RequestDAO requestDAO;
	private TransactionTemplate txTemplate;

	@Autowired
	@Qualifier("requestDAOImpl")
	public void setRequestDAO(RequestDAO requestDAO) {
		this.requestDAO = requestDAO;
	}

	@Autowired
	public void setTxTemplate(TransactionTemplate txTemplate) {
		this.txTemplate = txTemplate;
	}

	@Override
	public void create(final Integer from, final Integer to, final Integer community,
			final Boolean ownerRequest) {
		txTemplate.execute(new TransactionCallback<Void>() {
			public Void doInTransaction(TransactionStatus txStatus) {
				try {
					requestDAO.create(from, to, community, ownerRequest);
				} catch (RuntimeException e) {
					txStatus.setRollbackOnly();
					throw e;
				}
				return null;
			}
		});
	}

	@Override
	public Request getRequest(Integer id) {
		return requestDAO.getRequest(id);
	}

	@Override
	public List<Request> listAllRequests() {
		return requestDAO.listAllRequests();
	}

	@Override
	public List<Request> listAllToRequests(Integer to) {
		return requestDAO.listAllToRequests(to);
	}

	@Override
	public List<Request> listOwnerToRequests(Integer to) {
		return requestDAO.listOwnerToRequests(to);
	}

	@Override
	public List<Request> listNotOwnerToRequests(Integer to) {
		return requestDAO.listNotOwnerToRequests(to);
	}

	@Override
	public void delete(Integer id) {
		requestDAO.delete(id);
	}

	@Override
	public void update(Integer id, Integer from, Integer to, Integer community,
			Boolean ownerRequest) {
		requestDAO.update(id, from, to, community, ownerRequest);
	}

}
